#!/bin/bash
set -o errexit

# 用法说明
function usage(){
    echo "Usage: $0 <command> [options]"
    echo ""

    echo "  -h"
    echo "      显示命令使用帮助"
    echo "  change-ip <uuid> <ip> [-g <GATEWAY>] [-m <MAC>] [-i <INTERFACE>] [-v <VLAN_ID>]"
    echo "      uuid: vm uuid"
    echo "      ip: 更新后的ip地址"
    echo "      -g <GATEWAY> 网关, 只有在新旧ip不属于同一网关时需要"
    echo "      -m <MAC> 要更新ip的mac地址, 只有在多网卡且要更新的网卡不是默认网卡时需要"
    echo "      -i <INTERFACE> 要更新的网络接口, 只有在多网卡且要更新的网卡不是默认网卡时需要"
    echo "          当同时指定 -m 时, -i 不起作用"
    echo "      -v <VLAN_ID> 新ip的vlan id, 只有更改的ip与原ip不属于同一vlan id时需要"
    echo "     --help|-h 显示使用帮助，可选"
    echo ""
    echo "     本脚本在SmartOS(非Triton)下运行"   
    echo ""
    echo "     使用示例："
    echo "          $0 change-ip 1e65c912-3123-11eb-a066-4338f21c17e7 192.168.18.18"
    echo "              将虚拟机 1e65c912-3123-11eb-a066-4338f21c17e7 默认网卡的ip地址修改为 192.168.18.18"
    echo "          $0 change-ip 1e65c912-3123-11eb-a066-4338f21c17e7 192.168.18.18 -g 192.168.18.254"
    echo "              将虚拟机 1e65c912-3123-11eb-a066-4338f21c17e7 默认网卡的ip地址修改为 192.168.18.18, 网关修改为 192.168.18.254"
    echo "          $0 change-ip 1e65c912-3123-11eb-a066-4338f21c17e7 192.168.18.18 -m 22:68:e9:41:54:69"
    echo "              将虚拟机 1e65c912-3123-11eb-a066-4338f21c17e7 mac地址为 22:68:e9:41:54:69 的网卡的ip地址修改为 192.168.18.18"
    echo "          $0 change-ip 1e65c912-3123-11eb-a066-4338f21c17e7 192.168.18.18 -i eth0"
    echo "              将虚拟机 1e65c912-3123-11eb-a066-4338f21c17e7 接口为 eth0 的网卡的ip地址修改为 192.168.18.18"
    echo "          $0 change-ip 1e65c912-3123-11eb-a066-4338f21c17e7 192.168.18.18 -v 18"
    echo "              将虚拟机 1e65c912-3123-11eb-a066-4338f21c17e7 默认网卡的ip地址修改为 192.168.18.18, vlan id 修改为 18"
    echo "          $0 -h"
    echo "              显示命令使用帮助"

    echo "  <command>"
    echo "      转 vmadm <command>"
    exit
}

# 出错提示
function fatal(){
    echo ""
    echo "$1"
    exit 1
}


function change-vm-ip(){
    if [ $# -lt 3 ]; then
        echo "miss args"
        usage
    fi

    local vm_uuid=$2
    local new_ip=$3
    shift 3
    local gateway=""
    local mac_address=""
    local interface=""
    local vlan_id=""
    # 选项处理
    while [[ $# -ge 1 ]]; do
        case $1 in
            -g )
                gateway=$2
                shift 2
                ;;        
            -m )
                mac_address=$2
                shift 2
                ;;   
            -i )
                interface=$2
                shift 2
                ;;  
            -v )
                vlan_id=$2
                shift 2
                ;;    
            * )
                if [ $# -eq 1 ]; then
                    vm_uuid=$1
                    shift
                else
                    echo "不支持的参数 $1"
                    usage                
                fi
                ;;
        esac
    done

    # 预处理源虚拟机
    if [ "${vm_uuid}" == "" ]; then    
        echo "没有提供vm uuid "
        usage    
    fi

    vmadm list -Ho uuid|grep ${vm_uuid} 1>/dev/null || fatal "vm uuid ${vm_uuid} 不存在"

    if [ "${new_ip}" == "" ]; then    
        echo "没有提供ip地址"
        usage    
    fi

    if [ "${mac_address}" == "" ]; then
        if [ "${interface}" == "" ]; then
            mac_address=$(vmadm get ${vm_uuid}|json nics|json -c "this.primary==true" 0.mac)
            [[ "${mac_address}" != "" ]] || fatal "不存在要修改ip的网卡"
        else
            mac_address=$(vmadm get ${vm_uuid}|json nics|json -c "this.interface=='${interface}'" 0.mac)
            [[ "${mac_address}" != "" ]] || fatal "不存在要修改ip的网卡 interface[${interface}]"
        fi
    else
        local mac=$(vmadm get ${vm_uuid}|json nics|json -c "this.mac=='${mac_address}'" 0.mac)
        [[ "${mac}" != "" ]] || fatal "不存在要修改ip的网卡 mac[${mac_address}]"
    fi    

    local update_json="{\"update_nics\":[{\"mac\":\"${mac_address}\",\"ip\":\"${new_ip}\"}]}"
    [[ "${vlan_id}" == "" ]] || update_json=$(echo ${update_json} | json -e "update_nics[0].vlan_id=${vlan_id}" -o json-0)
    [[ "${gateway}" == "" ]] || update_json=$(echo ${update_json} | json -e "update_nics[0].gateway='${gateway}'" -o json-0)
    echo ${update_json} | vmadm update ${vm_uuid}
}


    
case $1 in
    change-ip )
        change-vm-ip $*
        ;;
    -h )
        usage
        ;;
    * )
        vmadm $*
        ;;
esac